<template>
  <el-dialog
    v-model="dialogVisible"
    title="绑定项目"
    width="600px"
  >
    <div class="manage-dialog">
      <div class="dialog-left">
        <!-- <el-input placeholder="组织机构" style="margin-bottom: 10px;"/> -->
        <el-tree
          :data="data.list"
          :props="defaultProps"
          @node-click="handleNodeClick"
        />
      </div>
      <div class="dialog-right">
        <el-input 
          placeholder="场景名称" 
          v-model="dataFilter.sceneName"
          @change="getSceneList"
          style="margin-bottom: 10px;"
        />
        <el-table
          :data="sourceData.list"
          border
          height="270px"
          highlight-current-row
          stripe
          @current-change="handleCurrentChange"
        >
          <el-table-column label="场景名称" prop="sceneName" />
          <el-table-column label="所属组织" prop="orgName" />
          <el-table-column label="已绑定场景类型" prop="sceneType" width="140"/>
        </el-table>
        <p style="color: #b8741a;">已绑定相同场景类型的项目不可选</p>
        
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button
          type="primary"
          @click="bindData"
        >确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { getOrg, getScene, addScene } from '@/api/settle'

const data = reactive({
  list: [] as any
})

const getOrgList = () => {
  getOrg({
    allFlag: true
  })
  .then((res: any)=>{
    console.log('resOrg', res.data)
    data.list = res.data
  })
  .catch((err: any)=>{
    console.error('err', err.msg)
  })
}

getOrgList()

const defaultProps = {
  children: 'children',
  label: 'organizationName',
}


const dialogVisible = ref(false)
const dialogStatus = ref('create')

const reset = () => {
  sceneId.value = null
}

const handleNodeClick = (data: any) => {
  dataFilter.orgId = data.organizationId
  getSceneList()
}

const emit = defineEmits(['listChange'])

const sceneId = ref(null)

const handleCurrentChange = (v: any) => {
  sceneId.value = v?.id
}

const bindData = async () => {
  if (sceneId.value) {
    addScene({
      sceneId: sceneId.value
    })
    .then(() => {
      // TODO use res data
      emit('listChange')
      ElMessage.success('绑定成功！')
      dialogVisible.value = false
    })
    .catch((err: any) => {
      dialogVisible.value = false
       ElMessage.error(err.msg)
    })
  }
}

const dataFilter = reactive({
  orgId: null,
  sceneName: '',
})

const pageProps = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0,
})

const sourceData = reactive({
  list: [] as any
})

const getSceneList = () => {
  getScene({
    pageNum: pageProps.value.pageNum,
    pageSize: pageProps.value.pageSize,
    ...dataFilter
  })
  .then((res: any)=>{
    sourceData.list = res.data
  })
  .catch((err: any)=>{
    console.error('err', err.msg)
  })
}


//点击取消
const close=()=>{
  dialogVisible.value = false
}
defineExpose({ dialogVisible, dialogStatus,sourceData,sceneId })

</script>
<style lang="scss" scoped>
.manage-dialog {
  position: relative;
  .dialog-left {
    position: absolute;
    width: 160px;
    padding-right: 10px;
    border-right: 1px solid #efefef;
    height: 360px;
    top: 0;
    left: 0;
    bottom: 0;
    overflow: scroll;
  }
  .dialog-right {
    height: 360px;
    margin-left: 160px;
    padding-left: 20px;
    overflow-y: scroll;
  }
}
</style>
